#include "euqt.h"
//bare minimum SQL connectivity using Qt driver
//need more study,
//note, QSqlDatabase is not a pointer and isn't a leak

//problem lasterror causes a dangling connection to any db plugin named...
//need remove and many many more things

#include <QtSql>

char * SQchar = new char [4096];

extern "C"
const char *qt_db_DriversAvailable(const char *drivername)
{
	return 0;
}

extern "C"
bool qt_db_isDriverAvailable(const char *drivername)
{
	return QSqlDatabase::isDriverAvailable(drivername);
}

//maybe only QSQLITE or QMYSQL or nothing depending on how compiled
//have to make sure whoever compiles euqt for distro has everything setup
extern "C"
bool qt_db_addDatabase(const char *drivername, const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::addDatabase(drivername);
	db.setDatabaseName(dbname);

	if (!db.open())
	{
		qDebug() << qApp->tr("in addDatabase Error %1 %2 %3").arg(drivername).arg(dbname).arg(db.lastError().text());
		return 0;
	}

	return 1;
}

extern "C"
bool qt_db_removeDatabase(const char *dbname)
{
	//should warn if not commit not finished?
	QSqlDatabase::removeDatabase(dbname);

	return 1;
}

extern "C"
const char *qt_db_lastError(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::addDatabase(dbname);
	strlcpy(SQchar, static_cast<const char*>(db.lastError().text().toUtf8().data()), 2000);//shareddata

	return SQchar;
}

//~ const char *dbname,
extern "C"
QSqlQuery *qt_db_Query(const char *q)
{
	QSqlQuery *query = new QSqlQuery();  //dbname, q
	return query;
}

//doesn't need const char *dbname, ?
extern "C"
bool qt_db_exec(QSqlQuery *query, const char *q)
{
	return query->exec(q);
}

extern "C"
bool qt_db_transaction(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::database(dbname);
	return db.transaction();
}
extern "C"
bool qt_db_rollback(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::database(dbname);
	return db.rollback();
}
extern "C"
bool qt_db_commit(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::database(dbname);
	return db.commit();
}
extern "C"
bool qt_db_isOpenError(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::database(dbname);
	return db.isOpenError();
}
extern "C"
bool qt_db_isOpen(const char *dbname)
{
	QSqlDatabase db = QSqlDatabase::database(dbname);
	return db.isOpen();
}

//couldn't make a pasteup macro with ->##name##()
extern "C"
bool qt_db_isActive(QSqlQuery *query)
{
	return query->isActive();
}
extern "C"
bool qt_db_isValid(QSqlQuery *query)
{
	return query->isValid();
}
extern "C"
bool qt_db_first(QSqlQuery *query)
{
	return query->first();
}
extern "C"
bool qt_db_next(QSqlQuery *query)
{
	return query->next();
}
extern "C"
bool qt_db_last(QSqlQuery *query)
{
	return query->last();
}
extern "C"
bool qt_db_previous(QSqlQuery *query)
{
	return query->previous();
}


extern "C"
bool qt_db_isForwardOnly(QSqlQuery *query)
{
	return query->isForwardOnly();
}

extern "C"
void qt_db_setForwardOnly(QSqlQuery *query, bool forward)
{
	query->setForwardOnly(forward);
}
extern "C"
bool qt_db_isSelect(QSqlQuery *query)
{
	return query->isSelect();
}

extern "C"
bool qt_db_isNull(QSqlQuery *query, int field)
{
	return query->isNull(field);
}

extern "C"
bool qt_db_seek(QSqlQuery *query, int index)
{
	return query->seek(index);
}

extern "C"
const char *qt_db_value(QSqlQuery *query, int index)
{
	QVariant x = query->value(index);
	strlcpy(SQchar, static_cast<const char*>(x.toString().toUtf8().data()), 2000);//shareddata
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SQchar;
}



#ifdef USESQL
#endif /* USESQL*/

